﻿using System;
using Sharp.Domain.CMSBoundedContext.VIUModule.Aggregates;
using Sharp.Domain.Repositories;
using Sharp.Infrastructure;

namespace Sharp.Domain.CMSBoundedContext.VIUModule.Services
{
    public class VIUService : IVIUService
    {
        private readonly IRepositoryContext _repositoryContext;
        private readonly IVIURepository _viuRepository;
        private readonly IApproveHistoryRepository _approveRepository;
        private readonly ISpecialRepository _specialRepository;


        public VIUService(IRepositoryContext repositoryContext,
            IVIURepository viuRepository,
            IApproveHistoryRepository approveRepository,
            ISpecialRepository specialRepository)
        {
            _repositoryContext = repositoryContext;
            _viuRepository = viuRepository;
            _approveRepository = approveRepository;
            _specialRepository = specialRepository;

        }
        /// <summary>
        /// 审核VIU
        /// </summary>
        /// <param name="viuId"></param>
        /// <param name="isChecked"></param>
        /// <param name="remark"></param>
        public Result CheckInVIU(Guid viuId, bool isChecked, string remark)
        {
            var res = new Result();
            if (!isChecked && remark.IsNullOrEmpty())
            {
                res.Err = "不通过审核，也不说点什么吗？";
                return res;
            }

            var viu = _viuRepository.Get(viuId);
            if (viu == null)
            {
                res.Err = "VIU不存在。";
                return res;
            }
            viu.IsChecked = isChecked;
            viu.ApproveHistories.Add(new ApproveHistory()
            {
                IsChecked = isChecked,
                Remark = remark,
                VIUId = viu.Id,
                CheckInTime = DateTime.Now,
                CheckInUserId = ContactContext.Current.UserId
            });

            _repositoryContext.Commit();
            return res;
        }  
        /// <summary>
        /// 审核VIU
        /// </summary>
        /// <param name="specialId"></param>
        /// <param name="isChecked"></param>
        /// <param name="remark"></param>
        public Result CheckInSpecial(Guid specialId, bool isChecked, string remark)
        {
            var res = new Result();
            if (!isChecked && remark.IsNullOrEmpty())
            {
                res.Err = "不通过审核，也不说点什么吗？";
                return res;
            }

            var special = _specialRepository.Get(specialId);
            if (special == null)
            {
                res.Err = "专题不存在。";
                return res;
            }
            special.IsChecked = isChecked;
            special.ApproveHistories.Add(new ApproveHistory4Special()
            {
                IsChecked = isChecked,
                Remark = remark,
                SpecialId = special.Id,
                CheckInTime = DateTime.Now,
                CheckInUserId = ContactContext.Current.UserId
            });

            _repositoryContext.Commit();
            return res;
        }
    }
}
